package ca.unbc.cpsc672;

import java.sql.SQLException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import ca.unbc.cpsc672.rcmd.BusinessInfo;
import ca.unbc.cpsc672.rcmd.DBUtils;

public class RcmdDialog extends Dialog {
	private BusinessInfo[] infoAry;

	public RcmdDialog(Shell parentShell, BusinessInfo[] buzInfo) {
		super(parentShell);
		this.infoAry = buzInfo;
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(GUIUtils.buildGridData());

		buildGroup(composite, "1st Choice", infoAry[0]);
		buildGroup(composite, "2nd Choice", infoAry[1]);
		buildGroup(composite, "3rd Choice", infoAry[2]);

		return super.createContents(parent);
	}

	private void buildGroup(Composite composite, String groupName,
			final BusinessInfo info) {
		if (info == null)
			return;
		Group group = new Group(composite, SWT.NONE);
		group.setText(groupName);
		group.setLayout(new GridLayout());
		group.setLayoutData(GUIUtils.buildGridData());
		Label label = new Label(group, SWT.NONE);
		StringBuffer sb = new StringBuffer();
		sb.append("Name: ").append(info.name).append("\n");
		sb.append("Address: ").append(info.address).append("\n");
		
		label.setText(sb.toString());
		Button btn = new Button(group, SWT.NONE);
		btn.setText("Like");
		Image img = new Image(composite.getDisplay(),
				RcmdDialog.class.getResourceAsStream("Facebook_like_thumb.png"));
		btn.setImage(img);
		btn.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					DBUtils.like(info.id);
					((Button)e.getSource()).setEnabled(false);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Rcommendations");
	}

	@Override
	protected void okPressed() {
		super.okPressed();
	}
}